<?php

namespace Common\Model;

class OrdersModel extends XuecheModel {

    protected $pk = 'order_id';
    protected $tableName = 'orders';
    protected $fields = array(
        'order_id', 'total_amount', 'pay_status', 'createtime', 'last_modified', 'ip', 'discount', 'tax_type', 'is_tax','goods_name','goods_price',
        'ship_mobile', 'ship_name', 'source', 'addon', 'mark_text', 'mark_type', 'disabled', 'memo', 'payed', 'pmt_order','coupon_code',
        'tax_company', 'tax_content','pay_time','cost_item', 'status', 'goods_id', 'tostr', 'formula', 'member_id','prepay_id'
    );

    /**
     * 清空数据缓存
     * 
     */
    public function ClearAllCache() {
        
    }

    //缓存设置
    protected $_useCacheType = 'file';
    protected $_catheTime = 0;

    public function Validate($data) {
        
        return true;
    }

    public function GetDataById($id = 0, $field = null) {
        return parent::GetDataById($id, $field);
    }

    public function SaveData($data, $pks = array()) {
        return parent::SaveData($data, $pks);
    }

    public function GetDataByWhere($where = '', $order = '', $start = 0, $rows = 30, $fields = '', $hasCount = TRUE, $bigData = FALSE) {
        return parent::GetDataByWhere($where, $order, $start, $rows, $fields, $hasCount, $bigData);
    }

    public function GetCount($where) {
        $where = " 1=1 " . $where;
        return $this->where($where)->count($this->pk);
    }
    
    /**
     * 用户删除订单
     * @param type $order_id
     */
    public function DelOne($order_id,$member_id){
        if(empty($order_id) || empty($member_id)){
            return false;
        }
        $data = array('disabled' => 'true','status' => 'dead');
        if($this->where("`order_id` = '{$order_id}' AND member_id = '{$member_id}'")->save($data)){
            return true;
        }
        return false;
    }
    
    /**
     * 订单支付完成处理
     * @param type $order_id
     * @param type $member_id
     * @return boolean
     */
    public function paySuccess($data){
        if(empty($data['order_id']) || empty($data['member_id'])){
            return FALSE;
        }
        $data = array('pay_status' => '1','last_modified' => time(),'payment'=>'s');
        if($this->where("`order_id` = '{$order_id}' AND member_id = '{$member_id}'")->save($data)){
            return true;
        }
        return false;
        
    }

}
